package com.nd.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nd.dao.CouponMapper;
import com.nd.dao.CouponUserMapper;
import com.nd.pojo.bean.*;
import com.nd.pojo.bo.CouponCreateBo;
import com.nd.pojo.bo.CouponDeleteBo;
import com.nd.pojo.vo.AdQueryDataVo;
import com.nd.pojo.vo.AdQueryVo;
import com.nd.pojo.vo.CouponQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    CouponMapper couponMapper;

    @Autowired
    CouponUserMapper couponUserMapper;


    @Override
    public AdQueryDataVo queryAll(Integer page, Integer limit, String sort, String order, String name, Short type, Short status) {

//        //执行查询之前  使用分页插件  会在执行过程中拼接上sql语句
//        PageHelper.startPage(page, limit);
//        //username = “test1”  nickname like 测试用户
//        //每个service的方法都来创建example对象
//        AdExample adExample = new AdExample();
//
//        //排序信息
//        adExample.setOrderByClause(sort + " " + order);
//        AdExample.Criteria criteria = adExample.createCriteria();
//
//        criteria.andDeletedEqualTo(false);
        PageHelper.startPage(page, limit);
        CouponExample couponExample = new CouponExample();

        //排序信息
        couponExample.setOrderByClause(sort + " " + order);
        CouponExample.Criteria criteria = couponExample.createCriteria();

        criteria.andDeletedEqualTo(false);

        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null && !"".equals(type)) {
            criteria.andTypeEqualTo(type);
        }
        if (status != null && !"".equals(status)) {
            criteria.andStatusEqualTo(status);
        }



        List<Coupon> coupons = couponMapper.selectByExample(couponExample);


        List<CouponQueryVo> couponQueryVos = new ArrayList<>();

        for (Coupon coupon : coupons) {
            CouponQueryVo couponQueryVo = new CouponQueryVo(coupon.getId(), coupon.getName(), coupon.getDesc(), coupon.getTag(),
                    coupon.getTotal(), coupon.getDiscount(), coupon.getMin(), coupon.getLimit(), coupon.getType(), coupon.getStatus(),
                    coupon.getGoodsType(), coupon.getGoodsValue(), coupon.getTimeType(),coupon.getDays(), coupon.getAddTime(), coupon.getUpdateTime(),
                    coupon.getDeleted());
            couponQueryVos.add(couponQueryVo);
        }


        //可以获得分页信息
        PageInfo<Coupon> userPageInfo = new PageInfo<>(coupons);
        long total = userPageInfo.getTotal();

        AdQueryDataVo couponQueryDataVo = new AdQueryDataVo();
        couponQueryDataVo.setTotal((int)total);
        couponQueryDataVo.setItems(coupons);


        return couponQueryDataVo;

    }

    @Override
    public Integer delete(CouponDeleteBo couponDeleteBo) {

        CouponExample couponExample = new CouponExample();

        CouponExample.Criteria criteria = couponExample.createCriteria();

        criteria.andIdEqualTo(couponDeleteBo.getId());

        Coupon coupon = new Coupon();
        coupon.setDeleted(true);

        Integer code = couponMapper.updateByExampleSelective(coupon, couponExample);

        return code;

    }

    @Override
    public Coupon queryById(Integer id) {

        CouponExample couponExample = new CouponExample();

        CouponExample.Criteria criteria = couponExample.createCriteria();

        criteria.andIdEqualTo(id);

        List<Coupon> coupons = couponMapper.selectByExample(couponExample);

        return coupons.get(0);
    }

    @Override
    public Coupon create(Coupon coupon) {

//        Coupon coupon = new Coupon();
//        coupon.setName(couponCreateBo.getName());
//        coupon.setDesc(couponCreateBo.getDesc());
//        coupon.setTag(couponCreateBo.getTag());

        if(coupon.getDays() < 0 || coupon.getTotal() < 0 || coupon.getDiscount().compareTo(BigDecimal.ZERO)==-1  ){
            return null;
        }

        Date date = new Date();

        coupon.setAddTime(date);
        coupon.setUpdateTime(date);

        int i = couponMapper.insertSelective(coupon);


        return coupon;
    }

    @Override
    public Coupon update(Coupon coupon) {

        if(coupon.getDays() < 0 || coupon.getTotal() < 0 || coupon.getDiscount().compareTo(BigDecimal.ZERO)==-1  ){
            return null;
        }

        coupon.setUpdateTime(new Date());

        CouponExample couponExample = new CouponExample();

        CouponExample.Criteria criteria = couponExample.createCriteria();

        criteria.andIdEqualTo(coupon.getId());

        couponMapper.updateByExampleSelective(coupon, couponExample);


        return coupon;
    }

    @Override
    public AdQueryDataVo queryUser(Integer page, Integer limit, String sort, String order, Integer couponId, Integer userId, Short status) {

        PageHelper.startPage(page, limit);
        CouponUserExample couponUserExample = new CouponUserExample();

        //排序信息
        couponUserExample.setOrderByClause(sort + " " + order);
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();

        criteria.andDeletedEqualTo(false);
        criteria.andCouponIdEqualTo(couponId);

        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (status != null && !"".equals(status)) {
            criteria.andStatusEqualTo(status);
        }




        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);


//        List<CouponQueryVo> couponQueryVos = new ArrayList<>();

//        for (Coupon coupon : coupons) {
//            CouponQueryVo couponQueryVo = new CouponQueryVo(coupon.getId(), coupon.getName(), coupon.getDesc(), coupon.getTag(),
//                    coupon.getTotal(), coupon.getDiscount(), coupon.getMin(), coupon.getLimit(), coupon.getType(), coupon.getStatus(),
//                    coupon.getGoodsType(), coupon.getGoodsValue(), coupon.getTimeType(),coupon.getDays(), coupon.getAddTime(), coupon.getUpdateTime(),
//                    coupon.getDeleted());
//            couponQueryVos.add(couponQueryVo);
//        }


        //可以获得分页信息
        PageInfo<CouponUser> userPageInfo = new PageInfo<>(couponUsers);
        long total = userPageInfo.getTotal();

        AdQueryDataVo couponQueryUserDataVo = new AdQueryDataVo();
        couponQueryUserDataVo.setTotal((int)total);
        couponQueryUserDataVo.setItems(couponUsers);


        return couponQueryUserDataVo;


    }
}
